package com.im.partchat.mvp.persenter;


import android.support.v4.app.Fragment;

import com.im.partchat.base.IBaseView;
import com.im.partchat.bean.request.RequestBean;
import com.im.partchat.mvp.model.news.NewsModel;
import com.im.partchat.mvp.model.news.NewsModelImpl;
import com.im.partchat.utils.rxlifecycle.compact.RxLifecycleCompact;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * Created by flyou on 2017/6/30.
 * VersionCode: 1
 * Desc:
 */

public class GetNewsPersenter<T> extends BasePresenter {
    private IBaseView<List<Object>> baseView;
    private NewsModel newsModel;
    private Fragment context;

    public GetNewsPersenter(IBaseView<List<Object>> baseView, Fragment fragment) {
        this.baseView = baseView;
        this.context = fragment;
        this.newsModel = new NewsModelImpl();
    }

    public void getNewList(@RequestBean.COLUMN final String column, int pageNo) {
        responseCompose(newsModel.getNews(column, pageNo), baseView, "获取新闻列表失败")
                .compose(RxLifecycleCompact.bind(context).<List<Object>>withObservable())
                .subscribe(new Observer<List<Object>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        baseView.showLoading("正在获取新闻列表");
                    }

                    @Override
                    public void onNext(List<Object> value) {

                        baseView.onLoadDataSuccess(value, column);
                    }

                    @Override
                    public void onError(Throwable e) {
                        baseView.hideLoading();
                        baseView.showError(NetErrorObservable(e));
                        e.printStackTrace();
                    }

                    @Override
                    public void onComplete() {
                        baseView.hideLoading();
                    }
                });
    }

    
}
